<?php
include_once "header.php";
include_once "date_utils.php";

$id_user=User::id();
$userData=User::userData();
$email = $userData["email"];
    
// This is used to submit new markers for review.
// Markers won't appear on the map until they are approved.

$contact_email = parseInput($_POST['contact_email']);
$title = parseInput($_POST['title']);
$type = parseInput($_POST['type']);
$address = parseInput($_POST['address']);
$uri = parseInput($_POST['uri']);
$description = parseInput($_POST['description'],true);
$start_date = parseInput($_POST['start_date']);
$end_date = parseInput($_POST['end_date']);
$hashtag = parseInput($_POST['hashtag']);


$today=date("Y-m-d");
$separator = "-";
$mysql_start_date = convertDateToDbFormat(  $start_date );
$mysql_end_date = convertDateToDbFormat(  $end_date);


// validate fields
if(empty($title) || empty($type) || empty($address) || empty($description) || empty($start_date) || empty($end_date)) {
  echo "Se requieren al menos los campos obligatorios.";
  exit;
} elseif(datecheck($start_date,$separator)===false) {
    echo "La fecha de inicio no es correcta";
    exit;
} elseif(datecheck($end_date,$separator)===false) { 
    echo "La fecha de fin no es correcta";
    exit;
} elseif($mysql_start_date > $mysql_end_date) {
  	 echo "Error, fecha de inicio mayor a la de fin.";
  	 exit;
} else {
    //¡¡¡ TEMPORAL  !!!
    //Si es un aviso, se añado como propuesta de Fran (con mail de info)
    if(!User::isAutentified()) {
      $id_user = 22;
      $email = $contact_email;
      $mail_subject = "Nueva protesta avisada";
      $mail_msg = "Alguien nos ha informado de una protesta<br><br>".
        "Email de contacto: $contact_email<br><br>";
    }else{
      $contact_email = $email;
      $mail_subject = "Nueva protesta subida"; 
      $mail_msg = "El siguiente usuario ha subido una protesta:<br><br>".
        $userData["email"]."<br>".
        $userData["nombre"]."<br>".
        $userData["organizacion"]."<br><br>";
    }
    
    $approved = (User::isAdmin())?1:2;
    
    // insert into db, wait for approval
    $insert = mysql_query("INSERT INTO places (id_user, approved, title, type, address, uri, description, owner_email, start_date, end_date, hashtag) VALUES ('$id_user', '$approved', '$title', '$type', '$address', '$uri', '$description', '$email', '$mysql_start_date', '$mysql_end_date', '$hashtag')") or die(mysql_error());
  
    // geocode new submission
    $hide_geocode_output = true;
    include "geocode.php";

    //Enviar email si no la ha subido un admin
    if(!User::isAdmin()){
      if (filter_var($contact_email, FILTER_VALIDATE_EMAIL)) {      
      
        $cabeceras  = 'MIME-Version: 1.0' . "\r\n";
        $cabeceras .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
        $cabeceras .= 'From: Manifiestate.net <info@manifiestate.net>' . "\r\n";
        $email = "info@manifiestate.net";
        
        mail($email, $mail_subject, $mail_msg."Título: $title<br>Tipo: $type<br>Ubicación: $address<br>Descripción: $description<br>Fecha inicio: $start_date<br>Fecha Fin: $end_date<br>URL: $uri<br>Hashtag: $hashtag", $cabeceras);

        echo (User::isAutentified())?"success":"mail_sent";
      } else {
        echo "Email de contacto incorrecto: $contact_email";
      }
    }else{
      echo "success";
    }
    
    exit;
}



?>
